/**
 *  *2010-8-17 下午03:22:06  add 
 */
package com.base.myproject.client.HR.account;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.HR.bcode.BcodeSelectDialog;
import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.ColumnInfo;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.busi.BO.TreeNodeModelData;
import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.Env;
import com.base.myproject.client.ui.BaseGridPanel;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.base.myproject.client.ui.dialog.ShowHtmlDialog;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.LabelField;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * 会计科目对照表设置
 * IBadminhd
 * @author Administrator 2010-6-25 javazgw@gmail.com
 */
public class setAccount extends BaseJDBCPanel {
	BaseComboBoxForm<BaseModelData> bsb_seach = new BaseComboBoxForm<BaseModelData>("mark","name","dictionary"," where type='hr_cw' ",true);
	BaseComboBoxForm<BaseModelData> bsb2 = new BaseComboBoxForm<BaseModelData>("mark","name","dictionary"," where type='hr_cw' ",true);
	BaseComboBoxForm<BaseModelData> bsbdep = new BaseComboBoxForm<BaseModelData>("id","name","systeminfo","",true);
	Button addbutton = new Button("添加");
	 //薪金项目
	 BaseComboBoxForm<BaseModelData> hr_Salary_Item_editor = new BaseComboBoxForm<BaseModelData>("code","desc_cn","hr_Salary_Item","",true);
	 BaseComboBoxForm<BaseModelData> hr_Salary_Item_seach = new BaseComboBoxForm<BaseModelData>("code","desc_cn","hr_Salary_Item","",true);
	 Button  query = new Button("查询");
	 DateField datef = new DateField();  
	public setAccount() {
		init();
	}

	public void init() {
		
		initWidgetX();
		addbutton.addListener(Events.Select, getListener());
		query.addListener(Events.Select, getListener());
		registerMainStore(store);
		registerStore(store);
	}

	private void initWidgetX() {
		add(initInnerToolBar());
		add(createGrid());
	}
	 BcodeSelectDialog<TreeNodeModelData> bsd_add ;

	 
	private VerticalPanel initInnerToolBar()
	{
		
		FieldSet fs = new FieldSet();
		fs.setHeading("查询操作");
		fs.setLayout(new ColumnLayout());
	
		fs.add(new LabelField("科目名称:"),new ColumnData(65));
		bsb_seach.setWidth(100);
		fs.add(bsb_seach,new ColumnData(120));
		
		fs.add(new LabelField("部门:"),new ColumnData(40));
		fs.add(bsbdep,new ColumnData(120));
		
		fs.add(new LabelField("薪金项目:"),new ColumnData(65));
		fs.add(hr_Salary_Item_seach,new ColumnData(120));
		
		
		fs.add(query,new ColumnData(60));
		fs.setWidth(990);
		fs.setHeight(60);
		fs.setBorders(true);
		
		FieldSet fs2 = new FieldSet();
		fs2.setHeading("添加操作");
		fs2.setLayout(new ColumnLayout());
		fs2.setWidth(990);
		fs2.setHeight(60);
		
		fs2.add(new LabelField("科目名称:"),new ColumnData(65));

		
		bsb2.setWidth(100);
		fs2.add(bsb2,new ColumnData(100));
		bsd_add = new BcodeSelectDialog<TreeNodeModelData>(){
			public void doposted(List<TreeNodeModelData> modeldatalist) {
				if(modeldatalist.size()>0)
				{
					String bcode_str="";
					for(int i=0;i<modeldatalist.size();i++){
						bcode_str=bcode_str+";"+modeldatalist.get(i).get("bname");
					}
					bcode_str=bcode_str.substring(1, bcode_str.length());
					this.setEmptyText(bcode_str);
				}
			}
		};
		bsd_add.setNeesreselect(false);
		bsd_add.setEmptyText("未选择数据");

		fs2.add(new LabelField("选择部门"),new ColumnData(65));
		bsd_add.setWidth(466);
		fs2.add(bsd_add,new ColumnData(100));
		
		fs2.add(new LabelField("薪金项目"),new ColumnData(65));
		hr_Salary_Item_editor.setWidth(112);
		fs2.add(hr_Salary_Item_editor,new ColumnData(100));
		
		fs2.add(addbutton);
		
		VerticalPanel vpanel = new VerticalPanel();
		vpanel.add(fs);
		vpanel.add(fs2);
		

		return vpanel;
	}
	BaseStore<BusiObject> store = null;
	BaseStore<BusiObject> getStore(){
	if(store==null) 
	{
		createStore();
	}
	return store;}
	BaseStore<BusiObject> createStore(){
	ModelType type = new ModelType();
	type.setRoot("records");
	type.setRecordName("record");
	type.setTotalName("totle");
	type.addField("icode");
	type.getField("icode").setType(Long.class);
	type.addField("accountNo");
	type.getField("accountNo").setType(String.class);
	type.addField("accountName");
	type.getField("accountName").setType(String.class);
	type.addField("depNo");
	type.getField("depNo").setType(String.class);
	type.addField("salaryItem");
	type.getField("salaryItem").setType(String.class);
	StoreInfo info = new StoreInfo();
	info.setTablename("hr_setAccount");
	info.setKeyname(new String[] { "icode"});
	info.setSql("select * from hr_setAccount");
	  store = new BaseStore<BusiObject>(type, info);
	 return store;}

	BaseGridPanel grid = null;
	BaseGridPanel createGrid() {
		CheckBoxSelectionModel<BusiObject> cbs = new CheckBoxSelectionModel<BusiObject>();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

		//ColumnConfig C1 = new ColumnConfig("icode", "icode", 80);
		//columns.add(C1);
		ColumnConfig C2 = new ColumnConfig("accountNo", "科目号码", 80);
		columns.add(C2);
		ColumnConfig C3 = new ColumnConfig("accountName", "科目名称", 80);
		columns.add(C3);
		ColumnConfig C4 = new ColumnConfig("depNo", "对应部门", 80);
		columns.add(C4);
		ColumnConfig C5 = new ColumnConfig("salaryItem", "薪金项目", 80);
		columns.add(C5);


		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGridPanel(getStore(), cm,true,true);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid.getBg().setSelectionModel(cbs);
		grid.setWidth(990);
		grid.getBg().setHeight(300);
		grid.getBg().setTrackMouseOver(true);
		grid.getBg().addListener(Events.RowDoubleClick, getListener());
		return grid;
	}

	int sync = 0;
	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
				try {
					if (sync == 1) {
						if (be.getType() == Events.Select) {
							
							if (be.getSource().equals(query)) {
								
								
								if(datef.getValue()!=null)
								{
							
								DateTimeFormat dtf = DateTimeFormat.getFormat("yyyy-MM-dd");
								dtf.format(datef.getValue());
								GWT.log(""+datef.getValue());
								
								String sql =" select * from hr_salary_ext  where datediff(month,'"+dtf.format(datef.getValue())+"',adddate )=0 order  by adddate desc";
								String sqlbefo = store.getStoreinfo().getSql();
								store.getStoreinfo().setSql(sql);
								store.reload();
								store.getStoreinfo().setSql(sqlbefo);
								}
							}
							//添加
							if (be.getSource().equals(addbutton)) {			
								final List<TreeNodeModelData>  selectchecklist=bsd_add.getTree().getCheckedSelection();
								//if(hr_Salary_Item_editor.getValueCode().equals("A20"))
								//{
									String bcodestr ="";
									
									String sql="select  name,mark   from  dictionary  where type='hr_cw' and name='"+bsb2.getRawValue()+"'";
									final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
									.create(com.base.myproject.client.tools.GreetingService.class);
							greetingService.getDataSet(
									sql, 
									new AsyncCallback<DataSet>() {

										@Override
										public void onFailure(Throwable caught) {
											MessageBox.alert("错误", "网络连接错误", null);
										}

										@Override
										public void onSuccess(DataSet result) {

											if (result.getRowCount()==0) {
												MessageBox.alert("", "错误，未找到科目名称", null);											}

											else {
												
													for(TreeNodeModelData tnmd:selectchecklist)
													{
														BusiObject bo = new BusiObject();
														bo.set("salaryItem", hr_Salary_Item_editor.getRawValue());
														bo.set("accountNo", result.getValue(0, "mark"));
														bo.set("accountName", bsb2.getRawValue());
														bo.set("depNo", tnmd.get("bname"));
														getStore().add(bo);
													}	
											}
										}

									});
									
									
								/**}
								//其他
								else
								{
								for(TreeNodeModelData tnmd:selectchecklist)
								{
									BusiObject bo = new BusiObject();
									bo.set("code", hr_Salary_Item_editor.getValueCode());
									//bo.set("amount", amount_set.getValue());
									bo.set("multiple", multiple_set.getValue());
									bo.set("bcode", tnmd.get("bcode"));
									//bo.set("bname", tnmd.get("bname"));
									bo.set("adddate", new Date());
									bo.set("createdate", new Date());
									bo.set("createbcode", Env.getEnvbcode());
									getStore().add(bo);
								}
								
								}**/
								bsd_add.deselectall();
								bsd_add.setEmptyText("未选择数据");
							}
						} else if (be.getType() == Events.Change) {
							if (be.getSource().equals("输入型变量")) {

							}
						} 
						else if (be.getType() == Events.RowDoubleClick) {
							if (be.getSource().equals(grid.getBg())) {
								GWT.log("RowDoubleClick");
								ShowHtmlDialog.getInstance().show("asdfas<a><p>"+grid.getBg().getValue("amount")+"</p>dd</a>", null);
							}
						}
						else if (be.getType() == Events.KeyUp) {
							if (be instanceof ComponentEvent) {
								if (((ComponentEvent) be).getKeyCode() == 13) // 回车
								{
									GWT.log("ddd");
								}
								else if(((ComponentEvent) be).isControlKey())
								{
									
									GWT.log("ctrl");
									
								}
									
							}
						}
					}
				} finally {
					sync--;
				}
			}

		};
		return defaultlistener;
	}

}
